package com.hyb.hybmodulesblog.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.hyb.hybmodulesblog.domain.Tribe;
import com.hyb.hybmodulesblog.domain.TribeUserVo;
import com.hyb.hybmodulesblog.service.ITribeService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.hyb.hybmodulesblog.domain.TribeUser;
import com.hyb.hybmodulesblog.service.ITribeUserService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 用户部落关联表增删改查Controller
 * 
 * @author hyb
 * @date 2023-03-11
 */
@RestController
@RequestMapping("/userTribe")
public class TribeUserController extends BaseController
{
    @Autowired
    private ITribeUserService tribeUserService;

    @Autowired
    private ITribeService tribeService;

    @Autowired
    private ITribeUserService userService;

    @GetMapping("/listTribeNumber")
    public AjaxResult listTribeNumber(@RequestParam("tribeNumber")String tribeNumber){
        //先查询该部落
        Tribe tribe = new Tribe();
        tribe.setNumber(tribeNumber);
        List<Tribe> tribes = tribeService.selectTribeList(tribe);
        if (tribes.size()==0){
            return AjaxResult.error("部落不存在");
        }
        tribe=tribes.get(0);
        List<TribeUserVo> list = tribeUserService.selectTribeUserDetail(tribe.getId());
        return AjaxResult.success().put("list",list);
    }

    /*
    * 查询某个部落下的用户
    * */
    @GetMapping("/listUser")
    public AjaxResult listUser(@RequestParam("tribeId")Long tribeId){

        List<TribeUserVo> list=tribeUserService.selectTribeUserDetail(tribeId);
        return AjaxResult.success().put("list",list);
    }

    /*
    * 查询某个人的所有部落
    * */
    @GetMapping("/listTribe")
    public AjaxResult listTribe(@RequestParam("userId")Long userId){
        List<com.ruoyi.system.api.domain.Tribe> list=tribeUserService.selectUserTribes(userId);

        return AjaxResult.success().put("list",list);
    }

    /**
     * 查询用户部落关联表增删改查列表
     */
    @RequiresPermissions("userTribe:userTribe:list")
    @GetMapping("/list")
    public TableDataInfo list(TribeUser tribeUser)
    {
        startPage();
        List<TribeUser> list = tribeUserService.selectTribeUserList(tribeUser);
        return getDataTable(list);
    }

    /**
     * 导出用户部落关联表增删改查列表
     */
    @RequiresPermissions("userTribe:userTribe:export")
    @Log(title = "用户部落关联表增删改查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TribeUser tribeUser)
    {
        List<TribeUser> list = tribeUserService.selectTribeUserList(tribeUser);
        ExcelUtil<TribeUser> util = new ExcelUtil<TribeUser>(TribeUser.class);
        util.exportExcel(response, list, "用户部落关联表增删改查数据");
    }

    /**
     * 获取用户部落关联表增删改查详细信息
     */
    @RequiresPermissions("userTribe:userTribe:query")
    @GetMapping(value = "/{tribeId}")
    public AjaxResult getInfo(@PathVariable("tribeId") Long tribeId)
    {
        return success(tribeUserService.selectTribeUserByTribeId(tribeId));
    }

    /**
     * 新增用户部落关联表增删改查
     */
    @RequiresPermissions("userTribe:userTribe:add")
    @Log(title = "用户部落关联表增删改查", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TribeUser tribeUser)
    {
        return toAjax(tribeUserService.insertTribeUser(tribeUser));
    }

    /**
     * 修改用户部落关联表增删改查
     */
    @RequiresPermissions("userTribe:userTribe:edit")
    @Log(title = "用户部落关联表增删改查", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TribeUser tribeUser)
    {
        return toAjax(tribeUserService.updateTribeUser(tribeUser));
    }

    /**
     * 删除用户部落关联表增删改查
     */
    @RequiresPermissions("userTribe:userTribe:remove")
    @Log(title = "用户部落关联表增删改查", businessType = BusinessType.DELETE)
	@DeleteMapping("/{tribeIds}")
    public AjaxResult remove(@PathVariable Long[] tribeIds)
    {
        return toAjax(tribeUserService.deleteTribeUserByTribeIds(tribeIds));
    }
}
